package com.xueyi.system.auxiliaryReceive.controller;

import com.xueyi.system.auxiliaryReceive.domain.query.AuxiliaryReceiveQuery;
import com.xueyi.system.auxiliaryReceive.domain.dto.AuxiliaryReceiveDto;
import com.xueyi.system.auxiliaryReceive.service.IAuxiliaryReceiveService;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.material.domain.dto.MaterialDto;
import com.xueyi.system.material.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 投入品领用管理 业务处理
 *
 * @author zlf
 */
@RestController
@RequestMapping("/auxiliaryReceive")
public class AuxiliaryReceiveController extends BaseController<AuxiliaryReceiveQuery, AuxiliaryReceiveDto, IAuxiliaryReceiveService> {


    @Autowired
    private IAuxiliaryReceiveService iAuxiliaryReceiveService;

    @Autowired
    private IMaterialService materialService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "投入品领用" ;
    }

    /**
     * 查询投入品领用列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.AUXILIARY_RECEIVE_LIST)
    public AjaxResult list(AuxiliaryReceiveQuery auxiliaryReceive) {
        return super.list(auxiliaryReceive);
    }

    /**
     * 查询投入品领用详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.AUXILIARY_RECEIVE_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 投入品领用导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.AUXILIARY_RECEIVE_EXPORT)
    @Log(title = "投入品领用管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, AuxiliaryReceiveQuery auxiliaryReceive) {
        super.export(response, auxiliaryReceive);
    }

    /**
     * 投入品领用新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.AUXILIARY_RECEIVE_ADD)
    @Log(title = "投入品领用管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody AuxiliaryReceiveDto auxiliaryReceive) {

        MaterialDto materialDto = materialService.selectById(auxiliaryReceive.getMaterialId());
//        if(auxiliaryReceive.getAuxiliaryNum().compareTo(materialDto.getQuantity()) > 0){
//            return AjaxResult.error("领用数量不能大于库存，请确认");
//        }
        BigDecimal subtract = materialDto.getQuantity().subtract(auxiliaryReceive.getAuxiliaryNum());
        materialDto.setQuantity(subtract);
        materialService.update(materialDto);
        return super.add(auxiliaryReceive);
    }

    /**
     * 投入品领用修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.AUXILIARY_RECEIVE_EDIT)
    @Log(title = "投入品领用管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody AuxiliaryReceiveDto auxiliaryReceive) {
        AuxiliaryReceiveDto auxiliaryReceiveDto = iAuxiliaryReceiveService.selectById(auxiliaryReceive.getId());
        MaterialDto materialDto = materialService.selectById(auxiliaryReceive.getMaterialId());
        if(auxiliaryReceive.getAuxiliaryNum().compareTo(auxiliaryReceiveDto.getAuxiliaryNum()) > 0){
            BigDecimal subtract = auxiliaryReceive.getAuxiliaryNum().subtract(auxiliaryReceiveDto.getAuxiliaryNum());
//            if(subtract.compareTo(materialDto.getQuantity()) > 0){
//                return AjaxResult.error("领用数量不能大于库存，请确认");
//            }
            BigDecimal subtract1 = materialDto.getQuantity().subtract(subtract);
            materialDto.setQuantity(subtract1);
        }else {
            BigDecimal subtract = auxiliaryReceiveDto.getAuxiliaryNum().subtract(auxiliaryReceive.getAuxiliaryNum());
            BigDecimal subtract1 = materialDto.getQuantity().add(subtract);
            materialDto.setQuantity(subtract1);
        }
        materialService.update(materialDto);
        return super.edit(auxiliaryReceive);
    }

    /**
     * 投入品领用批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.AUXILIARY_RECEIVE_DEL)
    @Log(title = "投入品领用管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        for (Long aLong : idList) {
            AuxiliaryReceiveDto auxiliaryReceiveDto = iAuxiliaryReceiveService.selectById(aLong);
            MaterialDto materialDto = materialService.selectById(auxiliaryReceiveDto.getMaterialId());
            BigDecimal subtract = materialDto.getQuantity().add(auxiliaryReceiveDto.getAuxiliaryNum());
            materialDto.setQuantity(subtract);
            materialService.update(materialDto);
        }

        return super.batchRemove(idList);
    }

    /**
     * 获取投入品领用选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 投入品领用管理 - 列表 */
        String AUXILIARY_RECEIVE_LIST = "system:auxiliaryReceive:list" ;
        /** 系统 - 投入品领用管理 - 详情 */
        String AUXILIARY_RECEIVE_SINGLE = "system:auxiliaryReceive:single" ;
        /** 系统 - 投入品领用管理 - 新增 */
        String AUXILIARY_RECEIVE_ADD = "system:auxiliaryReceive:add" ;
        /** 系统 - 投入品领用管理 - 修改 */
        String AUXILIARY_RECEIVE_EDIT = "system:auxiliaryReceive:edit" ;
        /** 系统 - 投入品领用管理 - 删除 */
        String AUXILIARY_RECEIVE_DEL = "system:auxiliaryReceive:delete" ;
        /** 系统 - 投入品领用管理 - 导出 */
        String AUXILIARY_RECEIVE_EXPORT = "system:auxiliaryReceive:export" ;
    }
}
